define(function (require,exports,module){
	/**--tmpl-require--**/

	var addmodule = require("admin/admin_roleadd");

	require('jtemplate');
	require('jdialog');
	common = require('common');

	exports.init = function(){
		/**--tmpl-todo--**/

		/**-- deleteopt --**/
		$(".js-delete").click(function(){
			var id = $(this).attr('data-id');
			layer.confirm('确定要删除吗',['确定','取消'],function(){
				layer.load();
				$.post(exports.deleteUrl,{id:id},function(data){
					layer.closeAll('loading');
					if(data.code == 0 || data.code=='success'){
						layer.msg('删除成功');
						document.location.reload();
					}else{
						layer.msg(data.message);
					}
				},'json');
			});
		});
		

		$(".js-edit").click(function(){
			id = $(this).attr('data-id');
			/**--tmpl-editlayer--**/
			$.get(exports.detailUrl,{id:id},function(data){
		
				var options = {
					type : 1,
					area : ['800px','500px'],
					content : JTemplate.render("editpanel",data),
					btn : {
						'确定' :function(){
							var form = $("#editpanel-form");
							form.validate({
								rules: {

										   /**--tmpl-editrules--**/

									   },
								messages : {  
											   /**--tmpl-editmessages--**/
										   },  

								submitHandler : function(form){

									$(form).find('input[name=id]').remove();
									$(form).append("<input type='hidden' name='id' value='"+id+"' >");
									var data = $(form).serialize();
									common.ajaxResult(exports.editUrl,data);

								}
							});
							form.submit();
						},
						"取消" : function(){
						
						}
					}
				};
				var options = $.extend(__global.jdialog, options);
				var popWin = JDialog.win.work(options);

				/**--tmpl-edittodo--**/

				/**-- assocfieldselect --**/
				if(typeof(addmodule.admin_manager_selected_ids == "undefined"))
				{
					addmodule.admin_manager_selected_ids = new Array();
				}
				if(data.admin_manager)
				{
					attr1 = '';
					attr2 = '';
					attr = "";
					for(var j in data.admin_manager[0]){
						if(j.indexOf('name') != -1){
							attr1 = j;
						}
						if(j.indexOf('title') != -1){
							attr2 = j;
						}
					}
					attr = attr1 ? attr1 : attr2;
					for(i = 0;i < data.admin_manager.length; i ++){
						id = data.admin_manager[i].id;
						name = data.admin_manager[i][attr];
						addmodule.admin_manager_selected_ids.push(id);
						ele = $('<li><input type="hidden" name="data[admin_manager][]" value="'+id+'">'+name+'<i class="glyphicon-remove glyphicon pull-right"></i></li>');
						$("#assoc-ul-admin_manager").append(ele);
					}
				}
				
				addmodule.admin_manager_assoc_url = exports.admin_manager_assoc_url;
				addmodule.assocadmin_manager();
				/**-- assocfieldselect --**/
				if(typeof(addmodule.admin_node_selected_ids == "undefined"))
				{
					addmodule.admin_node_selected_ids = new Array();
				}
				if(data.admin_node)
				{
					attr1 = '';
					attr2 = '';
					attr = "";
					for(var j in data.admin_node[0]){
						if(j.indexOf('name') != -1){
							attr1 = j;
						}
						if(j.indexOf('title') != -1){
							attr2 = j;
						}
					}
					attr = attr1 ? attr1 : attr2;
					for(i = 0;i < data.admin_node.length; i ++){
						id = data.admin_node[i].id;
						name = data.admin_node[i][attr];
						addmodule.admin_node_selected_ids.push(id);
						ele = $('<li><input type="hidden" name="data[admin_node][]" value="'+id+'">'+name+'<i class="glyphicon-remove glyphicon pull-right"></i></li>');
						$("#assoc-ul-admin_node").append(ele);
					}
				}
				
				addmodule.admin_node_assoc_url = exports.admin_node_assoc_url;
				addmodule.assocadmin_node();

				
			},'json');
			
		});
		$('select[name=action-bacth-select]').click(function (e) {

			if (!common.hasChecked) return;
			e.stopPropagation();

			var actionName=$(this).children('option:selected').val();

			switch(actionName){

				case 'delete':common.list_bacth_delete();break;
				case 'on':exports.list_bacth_on();break;
				case 'off':exports.list_bacth_off();break;
			}

		});


        $(".css-switch input").on("change",function(){
			var id = $(this).attr('data-id');
            var value = $(this).is(":checked") ?  'on' : 0;
			$.post(exports.editUrl,{id:id,data:{status : value}},function(data){
				if ( data.code == "000" ) {
					JDialog.tip.work({type:"ok", content:data.message, lock:true, timer:500});
				} else {
					JDialog.tip.work({type:"error", content:data.message, timer:1500,callback:function() {location.reload();}  });

				}
			},'json');

		});

	};

	exports.list_bacth_on=function(){
		var urlpathinfo = document.location.pathname.split('/');
		var url = '/' + urlpathinfo[1] + '/' + urlpathinfo[2] + '/do_updateStatus?type=1';
		var __confirm = JDialog.win.work({
			title: "启用",
			width: 340,
			height: 180,
			borderWidth: 8,
			lock: true,
			effect: 0,
			content: '<div style="padding-top: 15px;">确定要批量启用这些记录吗？</div>',
			icon: 'warn',
			button: {
				'确认': function () {
					$.post(url, $("#J_List_Form").serialize(), function (data) {
						__confirm.close();
						if (data.code == "000") {
							JDialog.tip.work({type: "ok", content: data.message, timer: 1000});
							setTimeout(function () {
								location.reload();
							}, 1000)
						} else {
							JDialog.tip.work({type: "error", content: data.message, timer: __global.jdialog.timer});
						}
					}, "json");
				},
				'取消': function () {
					__confirm.close();
				}
			}

		});


	};
	//批量禁用
	exports.list_bacth_off=function(){

		var urlpathinfo = document.location.pathname.split('/');
		var url = '/' + urlpathinfo[1] + '/' + urlpathinfo[2] + '/do_updateStatus?type=0';
		var __confirm = JDialog.win.work({
			title: "禁用",
			width: 340,
			height: 180,
			borderWidth: 8,
			lock: true,
			effect: 0,
			content: '<div style="padding-top: 15px;">确定要批量禁用这些记录吗？</div>',
			icon: 'warn',
			button: {
				'确认': function () {
					$.post(url, $("#J_List_Form").serialize(), function (data) {
						__confirm.close();
						if (data.code == "000") {
							JDialog.tip.work({type: "ok", content: data.message, timer: 1000});
							setTimeout(function () {
								location.reload();
							}, 1000)
						} else {
							JDialog.tip.work({type: "error", content: data.message, timer: __global.jdialog.timer});
						}
					}, "json");
				},
				'取消': function () {
					__confirm.close();
				}
			}

		});

	};
});
